home *** CD-ROM | disk | FTP | other *** search
- /*
- ** PortList.c PortList
- **
- ** This code is the property of Delta Tao Software, Inc.
- ** If you distribute this code or any derivative of it in any fashion
- ** we may and will come after your ass and take everything you have
- ** and everything you will every get anywhere in the universe for
- ** all of time.
- **
- ** **Warning** this code is god-like and perfect. When we work
- ** on it choirs of angels gather around and sing. Mortals must keep
- ** out. This code is so blindingly beautiful that your eyes will melt
- ** if you look at it.
- **
- ** Copyright (C) 1993 Delta Tao Software, Inc. All rights reserved.
- ** Use by license only. 408-730-9336.
- */
-
- #include "Types.h"
- #include "Traps.h"
- #include "OSUtils.h"
- #include "SegLoad.h"
- #include "Files.h"
- #include "Resources.h"
- #include "SysEqu.h"
-
-
- #pragma parameter __A0 GetInitHdl(__D0)
- pascal Handle GetInitHdl( void ) = { 0x4E71 };
-
- typedef pascal void (*GrafProcPtr)( void * );
-
- typedef struct GlobalsRec GlobalsRec, *GlobalsPtr;
- struct GlobalsRec
- {
- GrafProcPtr oldOpenPort;
- GrafProcPtr oldOpenCPort;
- GrafProcPtr oldInitPort;
- GrafProcPtr oldInitCPort;
- GrafProcPtr oldClosePort;
- GrafProcPtr oldCloseCPort;
- };
-
- typedef struct PortListRec PortListRec, *PortListPtr, **PortListHdl;
- struct PortListRec
- {
- short plCount;
- void * plPort[1];
- };
-
- #define gPortList (* (PortListHdl *) PortList)
-
- extern GlobalsPtr GetGlobals( void );
-
- static void * PatchTrap( short, void * );
- static pascal void PLOpenPort( void * );
- static pascal void PLOpenCPort( void * );
- static pascal void PLInitPort( void * );
- static pascal void PLInitCPort( void * );
- static pascal void PLClosePort( void * );
- static pascal void PLCloseCPort( void * );
- static void CheckExists( void *, short );
-
-
- /*
- ** PLPortList()
- */
- void
- PLPortList()
- {
- Handle hInit;
- GlobalsPtr gl;
-
- /* get the handle to the code */
- hInit = GetInitHdl();
-
- /* get pointer to globals */
- gl = GetGlobals();
-
- /* patch the traps */
- gl->oldOpenPort = PatchTrap( _OpenPort, PLOpenPort );
- gl->oldOpenCPort = PatchTrap( _OpenCPort, PLOpenCPort );
- gl->oldInitPort = PatchTrap( _InitPort, PLInitPort );
- gl->oldInitCPort = PatchTrap( _InitCPort, PLInitCPort );
- gl->oldClosePort = PatchTrap( _ClosePort, PLClosePort );
- gl->oldCloseCPort = PatchTrap( _CloseCPort, PLCloseCPort );
-
- /* we are installed */
- /* make sure the code does not leave when */
- /* the extension's resource fork is closed */
- DetachResource( hInit );
- }
-
-
- /*
- ** PatchTrap()
- */
- static void *
- PatchTrap( short trap, void * newProc )
- {
- void * oldProc;
-
- oldProc = (void *) GetToolTrapAddress( trap );
- SetToolTrapAddress( newProc, trap );
-
- return oldProc;
- }
-
-
- /*
- ** PLOpenPort()
- */
- static pascal void
- PLOpenPort( void * port )
- {
- CheckExists( port, false );
- (*GetGlobals()->oldOpenPort)( port );
- }
-
-
- /*
- ** PLOpenCPort()
- */
- static pascal void
- PLOpenCPort( void * port )
- {
- CheckExists( port, false );
- (*GetGlobals()->oldOpenCPort)( port );
- }
-
-
- /*
- ** PLInitPort()
- */
- static pascal void
- PLInitPort( void * port )
- {
- CheckExists( port, false );
- (*GetGlobals()->oldInitPort)( port );
- }
-
-
- /*
- ** PLInitCPort()
- */
- static pascal void
- PLInitCPort( void * port )
- {
- CheckExists( port, false );
- (*GetGlobals()->oldInitCPort)( port );
- }
-
-
- /*
- ** PLClosePort()
- */
- static pascal void
- PLClosePort( void * port )
- {
- CheckExists( port, true );
- (*GetGlobals()->oldClosePort)( port );
- }
-
-
- /*
- ** PLCloseCPort()
- */
- static pascal void
- PLCloseCPort( void * port )
- {
- CheckExists( port, true );
- (*GetGlobals()->oldCloseCPort)( port );
- }
-
-
- /*
- ** CheckExists()
- */
- static void
- CheckExists( void * port, short bFlag )
- {
- short count;
- void ** pPort;
- PortListPtr pPortList;
-
- /* walk the port list */
- pPortList = *gPortList;
- pPort = &pPortList->plPort[0];
- for ( count = pPortList->plCount - 1; count >= 0; --count )
- {
- /* if the port is is the list */
- if ( *pPort++ == port )
- {
- /* notify if port should not be in list */
- if ( bFlag == false )
- {
- DebugStr( "\pPort already in PortList." );
- }
- return;
- }
- }
-
- /* port is not in the list */
- /* notify if it should be */
- if ( bFlag )
- {
- DebugStr( "\pPort not in PortList." );
- }
- }
-